/*
 * FileName: InterruptThread.java
 * Author:   zzw
 * Date:     2018年05月12日
 * Description: 中断线程
 */
package com.zzw.thread;

/**
 * 〈中断线程〉<br>
 * 〈〉
 *
 * @author zzw
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本]（可选）
 */
public class InterruptThread extends Thread {

    @Override
    public void run() {
        while (true){
            if (this.isInterrupted()){
                System.out.println("Interrupted");
                break;
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println("Interrupted when sleep");
                this.interrupt();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptThread interruptThread = new InterruptThread();
        interruptThread.start();
        Thread.sleep(2000);
        interruptThread.interrupt();
    }
}
